package com.ruoyi.team.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.team.domain.Goods;
import com.ruoyi.team.domain.vo.TransferVo;
import com.ruoyi.team.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.team.mapper.GoodsCategoryMapper;
import com.ruoyi.team.domain.GoodsCategory;
import com.ruoyi.team.service.IGoodsCategoryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 物品分类信息Service业务层处理
 *
 * @author zj
 * @date 2022-11-27
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements IGoodsCategoryService {
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询物品分类信息列表
     *
     * @param goodsCategory 物品分类信息
     * @return 物品分类信息
     */
    @Override
    public List<GoodsCategory> selectGoodsCategoryList(GoodsCategory goodsCategory) {
        if (goodsCategory.getParentId() != null) {
            //查询上级分类
            GoodsCategory category = super.getById(goodsCategory.getParentId());
            //获取上级分类层级结构
            goodsCategory.getParams().put("busiPath", category.getBusiPath());
        }
        return getBaseMapper().selectGoodsCategoryList(goodsCategory);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean transfer(TransferVo transferVo) {

        if (transferVo.getSure() == null || !transferVo.getSure()) {
            if (transferVo.getSonId().equals(transferVo.getParentId())) {
                throw new ServiceException("不允许两个分类一样");
            }
            //不包含子类
            List<Goods> goods = goodsService.list(new QueryWrapper<Goods>().eq("category_id", transferVo.getSonId()));
            for (Goods good : goods) {
                good.setCategoryId(transferVo.getParentId() + "");
            }
            return goodsService.updateBatchById(goods);
        } else {
            //包含子类
            GoodsCategory category = getById(transferVo.getSonId());
            //构建查询条件
            GoodsCategory goodsCategory = new GoodsCategory();
            goodsCategory.getParams().put("busiPath", category.getBusiPath());
            //查询当前分类及其子类
            List<GoodsCategory> goodsCategories = getBaseMapper().selectGoodsCategoryList(goodsCategory);
            List<Long> longList = goodsCategories.stream().map(GoodsCategory::getId).collect(Collectors.toList());
            //查询物品
            List<Goods> goods = goodsService.list(new QueryWrapper<Goods>().in("category_id", longList));
            for (Goods good : goods) {
                good.setCategoryId(transferVo.getParentId() + "");
            }
            return goodsService.updateBatchById(goods);
        }
    }

    /**
     * 添加物品分类信息
     *
     * @param entity
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(GoodsCategory entity) {
        super.save(entity);
        String parentId = entity.getParentId();
        if (parentId == null) {
            entity.setBusiPath(entity.getId() + ":");
        } else {
            //查找父亲
            GoodsCategory category = super.getById(entity.getParentId());
            entity.setBusiPath(category.getBusiPath() + entity.getId() + ":");
        }

        return super.updateById(entity);
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeBatchByIds(Collection<?> list) {
        for (Object o : list) {
            GoodsCategory category = getById(Long.parseLong(o.toString()));
            int count = getBaseMapper().selectGoodStoreCount(Long.parseLong(o.toString()));
            if (count > 0) {
                throw new ServiceException("当前分类物品还有库存");
            }
            //查询当前分类的子类
            GoodsCategory goodsCategory = new GoodsCategory();
            goodsCategory.getParams().put("busiPath", category.getBusiPath());
            List<GoodsCategory> goodsCategories = getBaseMapper().selectGoodsCategoryList(goodsCategory);
            if (!CollectionUtils.isEmpty(goodsCategories)) {
                for (GoodsCategory goodsCategory1 : goodsCategories) {
                    int sum = getBaseMapper().selectGoodStoreCount(goodsCategory1.getId());
                    if (count > 0) {
                        throw new ServiceException("当前分类物品还有库存");
                    }
                }
            }
            list = goodsCategories.stream().map(GoodsCategory::getId).collect(Collectors.toList());
        }
        return super.removeBatchByIds(list);
    }
}
